<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/base.js"></script>
</head>
<body>
<h1 id="h1"></h1>
<h3 id="h3"></h3>
<div>
    <table id="userData" border="1px">

    </table>
</div>

    <script>
        function init() {
            let cid = localStorage.getItem("cid");
            let sSum = localStorage.getItem("sSum");
            let sMax = localStorage.getItem("sMax");
            document.getElementById("h1").innerHTML = "管理课程序号为" + cid + "的课程选课情况";
            document.getElementById("h3").innerHTML = "当前" + sSum + "人,容量" + sMax + "人";
            let url = "http://localhost:9001/Demo/course/users";
            let method = "post";
            let json = {};
            json.cid = cid;
            $.rest(method, url, json, function (r) {
                if (r.ok) {
                    //拿到users
                    let users = r.result.users;
                    let innerHTML = "<tr>\n" +
                        "            <td>学号</td>\n" +
                        "            <td>学生用户名</td>\n" +
                        "            <td>操作</td>\n" +
                        "        </tr>";
                    for (let i = 0; i < users.length; i++) {
                        let user = users[i];
                        let lineText = "<tr>\n" +
                            "            <td>" + user.id + "</td>\n" +
                            "            <td>" + user.username + "</td>\n" +
                            "            <td><a href='#' onclick='onDeleteClick(" + user.id + "," + cid + ")'>删除</a></td>\n" +
                            "        </tr>";
                        innerHTML += lineText;
                    }
                    document.getElementById("userData").innerHTML = innerHTML;

                }
            });
        }
        window.onload=init();
        function onDeleteClick(userId,cid) {
            let url="http://localhost:9001/Demo/user/course";
            let method="delete";
            let  json={};
            json.cid=cid;
            json.uid=userId;
            $.rest(method,url,json,function (r) {
                if(r.ok){
                    alert("删除成功");
                    init();
                }
            });
        }
    </script>
</body>
</html>